/**
 * Created by Administrator on 2016/6/21.
 */

//使按钮固定在底部
$(document).ready(function(){
    var $width = $(window).width();
    var $height = $(window).height();
    $(".button").css("top",$height-110);
    $(".button").css("display","block");
    $(".mask").css({'height':$height,'width':$width});
    $(".go_or_not2 ").css("top","700px");
    //图片显示切换
    $(".mainPart section:first-child").click(function(){
        $(".mask").toggle();
        $(".worthVaule").toggle();
        $(".close").toggle();
    })
    $(".close").click(function(){
        $(".mask").css("display","none");
        $(".worthVaule").css("display","none");
        $(".close").css("display","none");
    })

    $(".likeInput:nth-child(5)").click(function(){
        location.href="description.html";
    })
    $(".likeInput:nth-child(2) img").attr("src",sessionStorage.doctorimg);
    $(".likeInput:nth-child(2) p:nth-child(2) span:first-child").text(sessionStorage.doctorname);
    $(".likeInput:nth-child(2) p:nth-child(2) span:last-child").text(sessionStorage.doctorzhiwu);
    $(".likeInput:nth-child(2) p:nth-child(4)").text(sessionStorage.hospital_name);
});
$(document).ready(function(){
    console.log(sessionStorage.description,sessionStorage.imgNum);
    if((sessionStorage.description == ''||sessionStorage.description == undefined)&&(sessionStorage.imgNum == 0||sessionStorage.imgNum === undefined)){
        $("body section:nth-child(9)").css("display","none");
        $(".likeInput:nth-child(5)").css("display","block");
        $("header img").unbind();
        $("header img").click(function(){
            history.go(-1);
        })
    }else{
        $("header img").unbind();
        $("header img").click(function(){
            $(".go_or_not2").css("display","block");
            $(".mask").toggle();
        })
        $(".go_or_not2 p:nth-child(2)").click(function(){
            $(".go_or_not2").css("display","none");
            $(".mask").css("display","none");
        })
        $(".go_or_not2 p:nth-child(3)").click(function(){
            $(".go_or_not2").css("display","none");
            $(".mask").css("display","none");
            sessionStorage.imgNum=0;
            sessionStorage.img='';
            sessionStorage.description='';
            sessionStorage.minute = 0;
            history.go(-1);
        })
        //$("body section:nth-child(10)").css("display","none");
        $("body section:nth-child(5)").css("display","none");
        $("body section:nth-child(9)").css("display","block");
        $("body section:nth-child(9) p:first-child img").click(function(){
            location.href="description.html";
        })
        $("body section:nth-child(9) p:first-child span:last-child").click(function(){
            location.href="description.html";
        })
        if(sessionStorage.description !=''){
            $("section:nth-child(9)").css("display","block");
            $("body section:nth-child(9) p:nth-child(2)").text(sessionStorage.description);
        }else{
            $("body section:nth-child(9) p:nth-child(2)").css("display","none");
        }
        if(sessionStorage.imgNum != 0&&sessionStorage.imgNum !== undefined){
                $("section:nth-child(9)").css("display","block");
            var img =sessionStorage.img;
            var imgArray = img.split(";");
            console.log(imgArray);
            for(var i=0;i<imgArray.length;i++){
                var src= imgArray[i];
                var imgDiv= $("<section><img class=\"defaultImg\"></section>");
                imgDiv.children("img").attr("src",src);
                imgDiv.appendTo($(".imgGroup2"));
            }
        }else{
            $("body section:nth-child(9) section:nth-child(3)").css("display","none");
        }
    }
});
var u_id = sessionStorage.u_id;
var user_token =  sessionStorage.user_token;
//请求用户余额数据
var money;
$(document).ready(function(){
    var u_id = sessionStorage.u_id;
    postArray.u_id=u_id
    $.post(commonval+"/user_info",postArray,function(data){
        if(data.status==1){
            money = data.data.money/100;
            money = money.toFixed(2);
            var need =sessionStorage.tel_price*15/100-money;
            need = need.toFixed(2);
            //页面初始化数据加载
            //$("header p").text("电话咨询"+sessionStorage.d_name);
            $("section>section:nth-child(2) p:nth-child(3)").text("￥"+sessionStorage.tel_price/100+"/分钟");
            $("section>section:nth-child(4) p span:nth-child(1)").text(money);
            if(sessionStorage.tel_price*15/100>money){
                $("section>section:nth-child(4) p span:nth-child(2)").text(need);
            }else{
                $("section>section:nth-child(4) p span:nth-child(2)").text(0);
            }
            if($("section>section:nth-child(4) p span:nth-child(2)").text()== 0){
                $(".button button").removeAttr("disabled");
            }else{
                $(".button button").attr("disabled","true");
            }
        }else{
            if(data.msg=="非法请求"){
                createWindow("登录失效，正在跳转至登录界面");
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
                setTimeout(function(){location.href="register.html?jump=7"},4000);
            }
        }

    })
});
//咨询时长 计算逻辑
//长按事件
var timeout ;
var Interval ;
var consultTime =0;
$(".plusSign").on({
    touchstart: function(e){
        timeOutEvent = setTimeout(function() {
            Interval =  setInterval(function clock(){
                var need =sessionStorage.tel_price/100*(16+consultTime)-money;
                need = need.toFixed(2);
                consultTime++;
                $("section>section:nth-child(3) span:nth-child(2)").text(15+consultTime+"分钟");
                $("section>section:nth-child(3) img:nth-child(5)").attr("src","../images/minusSign02.png");
                var amount = $("section>section:nth-child(4) p span:nth-child(1)").text();
                if(sessionStorage.tel_price/100*(15+consultTime)>amount){
                    $("section>section:nth-child(4) p span:nth-child(2)").text(need);
                }else{
                    $("section>section:nth-child(4) p span:nth-child(2)").text(0);
                }
                if($("section>section:nth-child(4) p span:nth-child(2)").text()== 0){
                    $(".button button").removeAttr("disabled");
                }else{
                    $(".button button").attr("disabled","true");
                }
            },150);
        }, 400);
        $("section>section:nth-child(3) img:nth-child(5)").attr("src","../images/minusSign02.png");
        var amount = $("section>section:nth-child(4) p span:nth-child(1)").text();
        var need =sessionStorage.tel_price/100*(16+consultTime)-money;
        need = need.toFixed(2);
        consultTime++;
        $("section>section:nth-child(3) span:nth-child(2)").text(15+consultTime+"分钟");
        if(sessionStorage.tel_price/100*(15+consultTime)>amount){
            $("section>section:nth-child(4) p span:nth-child(2)").text(need);
        }else{
            $("section>section:nth-child(4) p span:nth-child(2)").text(0);
        }
        if($("section>section:nth-child(4) p span:nth-child(2)").text()== 0){
            $(".button button").removeAttr("disabled");
        }else{
            $(".button button").attr("disabled","true");
        }
        e.preventDefault();
    },
    touchend: function(e){
        clearTimeout(timeOutEvent);
        if(timeOutEvent!=0){
            clearTimeout(timeOutEvent);
            clearInterval(Interval);
        }
        e.preventDefault();
        return false;
    }
})
$(".minusSign").on({
    touchstart: function(e){
        timeOutEvent = setTimeout(function() {
            Interval =  setInterval(function clock(){
                if(consultTime>0){
                    var need =sessionStorage.tel_price/100*(14+consultTime)-money;
                    need = need.toFixed(2);
                    consultTime--;
                    $("section>section:nth-child(3) span:nth-child(2)").text(15+consultTime+"分钟");
                    var amount = $("section>section:nth-child(4) p span:nth-child(1)").text();
                    $("section>section:nth-child(3) span:nth-child(2)").text(15+consultTime+"分钟");
                    if(sessionStorage.tel_price/100*(15+consultTime)>amount){
                        $("section>section:nth-child(4) p span:nth-child(2)").text(need);
                    }else{
                        $("section>section:nth-child(4) p span:nth-child(2)").text(0);
                    }
                    if($("section>section:nth-child(4) p span:nth-child(2)").text()== 0){
                        $(".button button").removeAttr("disabled");
                    }else{
                        $(".button button").attr("disabled","true");
                    }
                }else{
                    $("section>section:nth-child(3) img:nth-child(5)").attr("src","../images/minusSign.png");

                }
            },150);
        }, 400);
        if(consultTime == 0){
            $("section>section:nth-child(3) img:nth-child(5)").attr("src","../images/minusSign.png");
        } else{
            var need =sessionStorage.tel_price/100*(14+consultTime)-money;
            need = need.toFixed(2);
           consultTime--;
            var amount = $("section>section:nth-child(4) p span:nth-child(1)").text();

            $("section>section:nth-child(3) span:nth-child(2)").text(15+consultTime+"分钟");
            if(sessionStorage.tel_price/100*(15+consultTime)>amount){
                $("section>section:nth-child(4) p span:nth-child(2)").text(need);
            }else{
                $("section>section:nth-child(4) p span:nth-child(2)").text(0);
            }
            if($("section>section:nth-child(4) p span:nth-child(2)").text()== 0){
                $(".button button").removeAttr("disabled");
            }else{
                $(".button button").attr("disabled","true");
            }
        }
        e.preventDefault();
    },
    touchend: function(e){
        clearTimeout(timeOutEvent);
        if(timeOutEvent!=0){
            clearTimeout(timeOutEvent);
            clearInterval(Interval);
        }
        return false;
        e.preventDefault();
    }
})
$(".plusSign").click(function(){

});
$("section>section:nth-child(3) img:nth-child(3)").click(function(){

});
$(document).ready(function(){

    //长按功能实现



    //$("section>section:nth-child(3) img:nth-child(3)").mouseout(function() {
    //    //timeout = clearTimeout(timeout);
    //    //Interval = clearInterval(Interval);
    //    alert("你好！");
    //});
    //$('section>section:nth-child(3) img:nth-child(3)').longPress(function(){
    //    setInterval(function clock(){
    //        consultTime++;
    //        $("section>section:nth-child(3) span:nth-child(2)").text(15+consultTime+"分钟");
    //    },200);
    //});

});
//充值跳转
$(document).ready(function(){
    $("section>section:nth-child(4) button").click(function(){
        var amount = $("section>section:nth-child(4) p span:nth-child(2)").text();
        location.href = "pay.html?amount="+amount;
        sessionStorage.jump_url=1;
    })
});
//点击图片调用uploadfive
$(document).ready(function(){
    $(".imgGroup .defaultImg").click(function(){
        $("#uploadifive-file_upload input:last-child").click();
    })
});
$.fn.longPress = function(fn) {
    var timeout = undefined;
    var $this = this;
    for(var i = 0;i<$this.length;i++){
        $this[i].addEventListener('touchstart', function(event) {
            timeout = setTimeout(fn, 800);  //长按时间超过800ms，则执行传入的方法
        }, false);
        $this[i].addEventListener('touchend', function(event) {
            clearTimeout(timeout);  //长按时间少于800ms，不会执行传入的方法
        }, false);
    }
}
//uploadfive参数传递回调
$(function() {
    var splik;
    $('#file_upload').uploadifive({
        'fileObjName': 'upload',
        'auto'             : true,
        'formData'         : {
            user_token:user_token,
            device:"wap",
            'category':"describe_imgs",
            'u_id':u_id
        },
        'removeCompleted' : true,
        'multi' : true,
        'uploadLimit' : 4,
        'simUploadLimit':10,
        'sizeLimit': 5120000,
        'fileType' : 'image/*',
        //'fileSizeLimit'   : 5242880,
        'queueID'          : 'queue',
        'uploadScript'     : commonval+'/uploadLiveImage',
        'onUploadComplete' : function(file, data) {
            splik = JSON.parse(data);
            //图片回调展示
            $(".upLoad img").attr("src","http://dev.haotoufa.com"+splik.data);
            $(".joinProcess2 #queue a").css("top","-25px");
            var img = $("<section><img class=\"userImg\"></section>");
            var src = "http://dev.haotoufa.com"+splik.data;
            img.children("img").attr("src",src);
            img.insertBefore($(".defaultImg").parent());
            //imgGroup展示区域
            var length = $(".imgGroup .userImg").length;
            if(length>3){
                $(".defaultImg").css("display","none");
            }
        }
    });
});

//ajax传递参数
$(document).ready(function(){
    //强制ajax同步
    $(".button button").click(function(){
        var i;
        var u_id = sessionStorage.u_id;
        var d_id = sessionStorage.d_id;
        console.log(sessionStorage.d_id);
        var describe = sessionStorage.description;
        var img =sessionStorage.img;
        var pic_num = sessionStorage.imgNum;
        if(pic_num == 0||pic_num === undefined){
            var imgArray = '';
        }else{
            var imgArray = img.split(";");
            var describe_img = imgArray[0].substr(length);
            for(var i=1;i<imgArray.length;i++){
                var src= imgArray[i];
                var srcUse = src.substr(length);
                describe_img = describe_img+";"+srcUse;
            }
        }



        var minuteDefault = $("section>section:nth-child(3) span:nth-child(2)").text();
        var minute = minuteDefault.replace("分钟", "");
        var cash = $("section>section:nth-child(4) p span:nth-child(2)").text();
        var str = window.location.search;
        var type = str.replace(/[^0-9]+/g, '');
        $.post(commonval+"/orderCreate",{
            d_id:d_id,
            u_id:u_id,
            user_token:user_token,
            device:"wap",
            describe:describe,
            describe_imgs:describe_img,
            pic_num:pic_num,
            type:type,
            minute:minute,
            session_id:session_id
        },function(data){
            if(data.status == "1"){
                var orderNumber = data.data.order_num;
                location.href ="orderSuccess.html?orderNumber="+orderNumber;
                sessionStorage.imgNum=0;
                sessionStorage.img='';
                sessionStorage.description='';
                sessionStorage.minute = minute;
            }else{
                createWindow(data.msg);
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
            }
        })
        console.log(type);
        if(cash != 0){
            if($(".showMsg").length > 0){
            }else{
                createWindow("抱歉，您的余额不足");
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
            }

        }else{
            if(describe == ''){
                if($(".showMsg").length > 0){

                }else{
                    createWindow("文字没有输入");
                    function createWindow(msg) {
                        var window =$("<section class='showMsg'></section>");
                        var show = "<p>"+msg+"</p>";
                        window.append(show);
                        $("body").append(window);
                        setTimeout("$('.showMsg').remove()",3000);
                    }
                }

            }else if(describe.length<20||describe.length>400){
                if($(".showMsg").length > 0){

                }else{
                    createWindow("您需要填写20-400的文字描述");
                    function createWindow(msg) {
                        var window =$("<section class='showMsg'></section>");
                        var show = "<p>"+msg+"</p>";
                        window.append(show);
                        $("body").append(window);
                        setTimeout("$('.showMsg').remove()",3000);
                    }
                }

            }else{
                if(describe_imgDefault ==undefined){
                    if($(".showMsg").length > 0){

                    }else{
                        createWindow("您还没上传图片");
                        function createWindow(msg) {
                            var window =$("<section class='showMsg'></section>");
                            var show = "<p>"+msg+"</p>";
                            window.append(show);
                            $("body").append(window);
                            setTimeout("$('.showMsg').remove()",3000);
                        }
                    }

                }else{
                    var str = window.location.search;
                    var type = str.replace(/[^0-9]+/g, '');
                    $.post("http://dev.haotoufa.com/orderCreate",{
                        d_id:d_id,
                        u_id:u_id,
                        user_token:user_token,
                        device:"wap",
                        describe:describe,
                        describe_imgs:describe_img,
                        pic_num:pic_num,
                        type:type,
                        minute:minute
                    },function(data){
                        if(data.status == "1"){
                            var orderNumber = data.data.order_num;
                            location.href ="orderSuccess.html?orderNumber="+orderNumber;
                            sessionStorage.minute = minute;
                        }else{
                            createWindow(data.msg);
                            function createWindow(msg) {
                                var window =$("<section class='showMsg'></section>");
                                var show = "<p>"+msg+"</p>";
                                window.append(show);
                                $("body").append(window);
                                setTimeout("$('.showMsg').remove()",3000);
                            }
                        }
                    })
                }
            }
        }
    })
});






